package game;

import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class SoundMenu extends Menu{

    public SoundMenu(Menu a) {
        super(a);
        init();
    }

    public SoundMenu(GrandeCanvas c) {
        super(c);
        init();
    }

    public void init(){
        if(getCanvas() == null){
            System.out.println("null");
        }
        
        elements.add(new MenuButton(this,(getCanvas().fwidth/2) - (200 + 25), (getCanvas().fheight/2) - 100, 200, 50,"Mute", 30, null){
        	@Override
        	public void clicked(MouseEvent e){
        		if(this.info.equals("Mute")){
        			getCanvas().players.get(0).suspend();
        			getCanvas().muted = true;
        			this.changeName("Unmute");
        		}
        		else if(this.info.equals("Unmute")){
        			getCanvas().players.get(0).resume();
        			getCanvas().muted = false;
        			this.changeName("Mute");
        		}
        	}
        });
        elements.add(new MenuButton(this, (getCanvas().fwidth/2) + 25, (getCanvas().fheight/2) - 100, 200, 50,"Change Song", 30, null){
            @Override
            public void clicked(MouseEvent e){
                JOptionPane option = new JOptionPane();
                Object[] possibleValues = { "Defult", "Play Your Own"};
                Object selectedValue = option.showInputDialog(null,
                        "Choose one", "Choose music",
                        JOptionPane.INFORMATION_MESSAGE, null,
                        possibleValues, possibleValues[0]);
                try{
                    if(selectedValue.equals("Defult")){
                        getCanvas().stopMusic(getCanvas().players.get(0));
                        getCanvas().addMusic(new OggPlayer(new File("images/2nd Flight.ogg"), .7f));
                    }
                    if(selectedValue.equals("Play Your Own")){
                        JFileChooser fc = new JFileChooser();
                        OggFileFilter filter = new OggFileFilter();
                        fc.addChoosableFileFilter(filter);
                        fc.setAcceptAllFileFilterUsed(false);
                        int val = fc.showOpenDialog(null);
                        if(val == fc.APPROVE_OPTION && filter.accept(fc.getSelectedFile())){
                            File newMusic = fc.getSelectedFile();
                            getCanvas().stopMusic(getCanvas().players.get(0));
                            getCanvas().addMusic(new OggPlayer(newMusic, .7f));
                        }
                    }
                }
                catch(NullPointerException a){
                }
            }
        });
        elements.add(new MenuSlider(this, (getCanvas().fwidth/2) - (450/2), (getCanvas().fheight/2) - 25, 450, 50, "Volume", 30){
            public void update(){
            	setValue(getCanvas().settings.volume);
            	super.update();
            	getCanvas().settings.volume = getPosition();
            	getCanvas().players.get(0).setVolume(getCanvas().settings.volume);
            }
        });
        elements.add(new MenuSlider(this, (getCanvas().fwidth/2) - (450/2), (getCanvas().fheight/2) + 50, 450, 50, "Sound Volume", 30){
        	public void update(){
        		setValue(getCanvas().settings.soundVolume);
        		super.update();
        		getCanvas().settings.soundVolume = getPosition();
        	}
        });
        elements.add(new MenuButton(this, (getCanvas().fwidth/2) - (450/2), (getCanvas().fheight/2) + 125, 450, 50, "Back", 30, null){
            @Override
            public void clicked(MouseEvent e){
                if(parent != null){
                    parent.subMenu = null;
                }
            }
        });
    }
}
